package com.example.mall.controller;

import com.example.mall.dto.response.BaseResponse;
import com.example.mall.model.CommodityInfo;
import com.example.mall.model.Order;
import com.example.mall.model.Tag;
import com.example.mall.service.CommoityInfoService;
import com.example.mall.service.SellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@ResponseBody
@RequestMapping("/sellinfo")
public class SellInfoController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SellService sellService;
    @Autowired
    private CommoityInfoService commoityInfoService;


    private String sql;
    private Integer money;
    private Map<String, Integer> map;
    private RowMapper<Order> rowMapper = new BeanPropertyRowMapper<>(Order.class);
    private List<Order> list = new ArrayList<>();

    public void getMoneyAndCountByIdAndDate(Integer sID, String dateTag, Integer startTime, Integer endTime) {
        sql = "select distinct `order`.* from `order`,contain,commodityinfo where UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) < " + 86400 * endTime + " and UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) > " +  86400*startTime + "  and `order`.id = contain.oID and contain.cID = commodityinfo.id and commodityinfo.sellerID =" + sID;
        list = jdbcTemplate.query(sql, rowMapper);
        map.put(dateTag + "SoldNum", list.size());
        sql = "select sum(commodityinfo.price * num) from `order`,contain,commodityinfo where UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) < " + 86400 * endTime + " and UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) > " +  86400*startTime + "  and `order`.id = contain.oID and contain.cID = commodityinfo.id and commodityinfo.sellerID = " + sID;
        money = jdbcTemplate.queryForObject(sql, Integer.class);
        if (money == null) {
            money = 0;
        }
        map.put(dateTag + "SoldMoney", money);
    }

    public void getOrderCountByState(String state,String desc,Integer sID){
        map.put(desc + "Count",sellService.getOrderByState(state,sID).size());
    }

    public void getPersonCountByIdAndDate(Integer sID, String dateTag, Integer startTime, Integer endTime) {
        sql = "select count(distinct buyerID) from `order`,contain,commodityinfo where UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) < " + 86400 * endTime + " and UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(`timestamp`) > " +  86400*startTime + "  and `order`.id = contain.oID and contain.cID = commodityinfo.id and commodityinfo.sellerID =" + sID;
        list = jdbcTemplate.query(sql, rowMapper);
        map.put(dateTag + "PersonCount", list.size());
    }

    @GetMapping("info")
    public BaseResponse getSellInfo(@RequestParam("sID") Integer sID) {
        map = new HashMap<>();
        getMoneyAndCountByIdAndDate(sID,"today",0,1);
        getMoneyAndCountByIdAndDate(sID,"yesterday",1,2);
        getMoneyAndCountByIdAndDate(sID,"sevenDays",0,7);
        getMoneyAndCountByIdAndDate(sID,"lastSevenDays",7,14);
        getOrderCountByState("未付款","unpaid",sID);
        getOrderCountByState("已付款","paid",sID);
        getOrderCountByState("已完成","complete",sID);
        getOrderCountByState("待退款","unRefund",sID);
        getOrderCountByState("已退款","refund",sID);
        getPersonCountByIdAndDate(sID,"today",0,1);
        getPersonCountByIdAndDate(sID,"yesterday",1,2);
        getPersonCountByIdAndDate(sID,"sevenDays",0,7);
        getPersonCountByIdAndDate(sID,"total",0,Integer.MAX_VALUE);
        getCommodityInfo(sID);
        getTagInfo(sID);
        return new BaseResponse(200, "mySoldData", map);
    }

    public void getTagInfo(Integer sID){
        map.put("tagCount",getTagBySellerID(sID).size());
    }

    @PostMapping("taglist")
    public List<Tag> getTagBySellerID(@RequestParam("sID") Integer cID){
        String sql = "select distinct tag.* from commodityinfo,belong,tag where commodityinfo.id = belong.cID and tag.id = belong.tID and commodityinfo.sellerID = ?;";
        RowMapper<Tag> rowMapper = new BeanPropertyRowMapper<>(Tag.class);
        return jdbcTemplate.query(sql,rowMapper,cID);
    }

    public void getCommodityInfo(Integer sID){
        map.put("totalCommodityCount",getCommodityList(sID,0,Integer.MAX_VALUE).size());
        map.put("warningCommodityCount",getCommodityList(sID,0,10).size());
    }


    private List<CommodityInfo> getCommodityList(Integer sID,Integer minValue,Integer maxValue){
        String sql = "select * from commodityinfo where sellerID = ? and remain > ? and remain < ?;";
        RowMapper<CommodityInfo> rowMapper = new BeanPropertyRowMapper<>(CommodityInfo.class);
        return jdbcTemplate.query(sql,rowMapper,sID,minValue,maxValue);
    }

    @PostMapping("commodityList")
    public BaseResponse getCommodityList(@RequestParam("sID")Integer sID){
        List<CommodityInfo> list = commoityInfoService.getCommodityList(sID);
        return new BaseResponse(200,"ok",list);
    }

    @PostMapping("saleinfo")
    public BaseResponse getSaleInfo(@RequestParam("begin")String begin,@RequestParam("end")String end,@RequestParam("sID") Integer sID) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return new BaseResponse(200,"orderStatistics",sellService.getStatistics(sID,df.parse(begin),df.parse(end)));
    }
}
